<?php
function gfci_wall_multi_register() {
	if ( !$options = get_option('wall_widget_gfci') )
		$options = array();
	$widget_ops = array('classname' => 'wall_widget_gfci', 'description' => __('Add a Google Friend Connect Wall Gadget'));
	$control_ops = array('width' => 420, 'height' => 420, 'id_base' => 'gfciwall');
	$name = __('GFC Comments');

	$registered = false;
	foreach ( array_keys($options) as $o ) {
		// Old widgets can have null values for some reason
		if ( !isset($options[$o]['gadget_wid']) )
			continue;
		$id = "gfciwall-$o"; // Never never never translate an id
		$registered = true;
		wp_register_sidebar_widget($id, $name, 'GFCIWidget_Wall', $widget_ops, array( 'number' => $o ));
		wp_register_widget_control($id, $name, 'widget_gfci_wall_control', $control_ops, array( 'number' => $o ));
	}
	
	// If there are none, we register the widget's existance with a generic template
	if ( !$registered ) {
            wp_register_sidebar_widget('gfciwall-1', $name, 'GFCIWidget_Wall', $widget_ops, array( 'number' => -1 ) );
            wp_register_widget_control('gfciwall-1', $name, 'widget_gfci_wall_control', $control_ops, array( 'number' => -1 ) );
        }
}

function GFCIWidget_Wall($args, $widget_args = 1) {
	extract( $args, EXTR_SKIP );
	if ( is_numeric($widget_args) )
		$widget_args = array( 'number' => $widget_args );
	$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
	extract( $widget_args, EXTR_SKIP );
	$canvas_path = get_option( gfci_canvas_path );
	$options = get_option('wall_widget_gfci');
	if ( !isset($options[$number]) )
		return;
        $gadget_wid = attribute_escape($options[$number]['gadget_wid']);
	$gadget_hei = attribute_escape($options[$number]['gadget_hei']);
	$endcap_bkgd = attribute_escape($options[$number]['endcap_bkgd']);
	$endcap_text = attribute_escape($options[$number]['endcap_text']);
	$endcap_links = attribute_escape($options[$number]['endcap_links']);
	$content_bkgd = attribute_escape($options[$number]['content_bkgd']);
	$content_text = attribute_escape($options[$number]['content_text']);
	$content_text2 = attribute_escape($options[$number]['content_text2']);
	$content_links = attribute_escape($options[$number]['content_links']);
	$content_links2 = attribute_escape($options[$number]['content_links2']);
	$content_hlines = attribute_escape($options[$number]['content_hlines']);
	$alt_bkgd = attribute_escape($options[$number]['alt_bkgd']);
	$gfci_border = attribute_escape($options[$number]['gfci_border']);
	$header_text = attribute_escape($options[$number]['header_text']);
	$com_text = attribute_escape($options[$number]['com_text']);
	$video_links = attribute_escape($options[$number]['video_links']);
	$anon_post = attribute_escape($options[$number]['anon_post']);
	$scope = attribute_escape($options[$number]['scope']);
	$wallid = attribute_escape($options[$number]['wallid']);
  echo $before_widget;
  echo $before_title;
  echo $after_title;
?>
<div id="gfcwall-<?php echo $number; ?>" style="width:<?php echo $gadget_wid; ?>px;border:1px solid #cccccc;"></div>
<script type="text/javascript">
var skin = {};
skin['BORDER_COLOR'] = '<?php echo $gfci_border; ?>';
skin['ENDCAP_BG_COLOR'] = '<?php echo $endcap_bkgd; ?>';
skin['ENDCAP_TEXT_COLOR'] = '<?php echo $endcap_text; ?>';
skin['ENDCAP_LINK_COLOR'] = '<?php echo $endcap_links; ?>';
skin['ALTERNATE_BG_COLOR'] = '<?php echo $alt_bkgd; ?>';
skin['CONTENT_BG_COLOR'] = '<?php echo $content_bkgd; ?>';
skin['CONTENT_LINK_COLOR'] = '<?php echo $content_links; ?>';
skin['CONTENT_TEXT_COLOR'] = '<?php echo $content_text; ?>';
skin['CONTENT_SECONDARY_LINK_COLOR'] = '<?php echo $content_links2; ?>';
skin['CONTENT_SECONDARY_TEXT_COLOR'] = '<?php echo $content_text2; ?>';
skin['CONTENT_HEADLINE_COLOR'] = '<?php echo $content_hlines; ?>';
skin['DEFAULT_COMMENT_TEXT'] = '<?php echo $com_text; ?>';
skin['HEADER_TEXT'] = '<?php echo $header_text; ?>';
skin['POSTS_PER_PAGE'] = '<?php echo $gadget_hei; ?>';
google.friendconnect.container.setParentUrl('<?php echo $canvas_path; ?>' /* location of rpc_relay.html and canvas.html */);
google.friendconnect.container.renderWallGadget(
 { id: 'gfcwall-<?php echo $number; ?>',
   site: '<?php echo gfci_sit(); ?>',
   'view-params':{"scope":"<?php echo $scope; ?>","allowAnonymousPost":"<?php echo $anon_post; ?>","features":"<?php echo $video_links; ?>comment","docId":"<?php echo $wallid; ?>"}
},
  skin);
</script>
<?php
  echo $after_widget;
}

function widget_gfci_wall_control($widget_args) {
	global $wp_registered_widgets;
	static $updated = false; // Whether or not we have already updated the data after a POST submit

	if ( is_numeric($widget_args) )
		$widget_args = array( 'number' => $widget_args );
	$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
	extract( $widget_args, EXTR_SKIP );

	// Data should be stored as array:  array( number => data for that instance of the widget, ... )
	$options_all = get_option('wall_widget_gfci');
	if ( !is_array($options_all) )
		$options_all = array();

	if ( !$updated && !empty($_POST['sidebar']) ) {
		// Tells us what sidebar to put the data in
		$sidebar = (string) $_POST['sidebar'];

		$sidebars_widgets = wp_get_sidebars_widgets();
		if ( isset($sidebars_widgets[$sidebar]) )
			$this_sidebar =& $sidebars_widgets[$sidebar];
		else
			$this_sidebar = array();

		foreach ( $this_sidebar as $_widget_id ) {
		if ( 'wall_widget_gfci' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
				$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
				if (!in_array("gfciwall-$widget_number", $_POST['widget-id']))
				unset($options_all[$widget_number]);
			}
		}
                foreach ( (array) $_POST['gfc-wall-post'] as $widget_number => $posted ) {
			if (!isset($posted['gadget_wid']) && isset($options_all[$widget_number]))
                    continue;
                
                $options = array();
                $options['gadget_wid'] = $posted['gadget_wid'];
		$options['gadget_hei'] = $posted['gadget_hei'];
		$options['endcap_bkgd'] = $posted['endcap_bkgd'];
		$options['endcap_text'] = $posted['endcap_text'];
		$options['endcap_links'] = $posted['endcap_links'];
		$options['content_bkgd'] = $posted['content_bkgd'];
		$options['content_text'] = $posted['content_text'];
		$options['content_text2'] = $posted['content_text2'];
		$options['content_links'] = $posted['content_links'];
		$options['content_links2'] = $posted['content_links2'];
		$options['content_hlines'] = $posted['content_hlines'];
		$options['header_text'] = $posted['header_text'];
		$options['gfci_border'] = $posted['gfci_border'];
		$options['com_text'] = $posted['com_text'];
		$options['video_links'] = $posted['video_links'];
		$options['anon_post'] = $posted['anon_post'];
		$options['scope'] = $posted['scope'];
		$options['wallid'] = $posted['wallid'];
		$options['alt_bkgd'] = $posted['alt_bkgd'];
		$options_all[$widget_number] = $options;
            }
            update_option('wall_widget_gfci', $options_all);
            $updated = true;
	    
}
	if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
		$gadget_wid = '282';
		$number = '%i%';
		$gadget_hei = '5';
		$endcap_bkgd = 'e0ecff';
		$endcap_text = '333333';
		$endcap_links = '0000cc';
		$content_bkgd = 'ffffff';
		$content_text = '333333';
		$content_text2 = '666666';
		$content_links = '0000cc';
		$content_links2 = '7777cc';
		$content_hlines = '333333';
		$header_text = 'Comments';
		$gfci_border = 'cccccc';
		$com_text = '- add your comment here -';
		$video_links = 'video,';
		$anon_post = 'true';
		$scope = 'SITE';
		$wallid = '';
		$alt_bkgd = 'ffffff';
		
	} else {
		$gadget_wid = $options_all[$number]['gadget_wid'];
		$gadget_hei = $options_all[$number]['gadget_hei'];
		$endcap_bkgd = $options_all[$number]['endcap_bkgd'];
		$endcap_text = $options_all[$number]['endcap_text'];
		$endcap_links = $options_all[$number]['endcap_links'];
		$content_bkgd = $options_all[$number]['content_bkgd'];
		$content_text = $options_all[$number]['content_text'];
		$content_text2 = $options_all[$number]['content_text2'];
		$content_links = $options_all[$number]['content_links'];
		$content_links2 = $options_all[$number]['content_links2'];
		$content_hlines = $options_all[$number]['content_hlines'];
		$header_text = $options_all[$number]['header_text'];
		$gfci_border = $options_all[$number]['gfci_border'];
		$com_text = $options_all[$number]['com_text'];
		$video_links = $options_all[$number]['video_links'];
		$anon_post = $options_all[$number]['anon_post'];
		$scope = $options_all[$number]['scope'];
		$wallid = $options_all[$number]['wallid'];
		$alt_bkgd = $options_all[$number]['alt_bkgd'];
	}

?>
<h3>Wall Width and Options</h3>
<table>
<tr>
<td><label for="<?php echo $number; ?>-gadget-wid">
  Width:
  </label></td>
<td><input class="widefat" 
    type="text"
    name="gfc-wall-post[<?php echo $number; ?>][gadget_wid]" 
    id="<?php echo $number; ?>-gadget-wid" 
    value="<?php echo $gadget_wid; ?>"/></td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-gadget-hei">
  Comments Per Page:
  </label></td>
<td><input class="widefat" type="text" 
    name="gfc-wall-post[<?php echo $number; ?>][gadget_hei]" 
    id="<?php echo $number; ?>-gadget-hei" 
    value="<?php echo $gadget_hei; ?>"/></td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-header-text">
  Header Text:
  </label></td>
<td><input class="widefat" type="text" 
    name="gfc-wall-post[<?php echo $number; ?>][header_text]" 
    id="<?php echo $number; ?>-header-text" 
    value="<?php echo $header_text; ?>"/></td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-com-text">
  Default Text:
  </label></td>
<td><input class="widefat" type="text" 
    name="gfc-wall-post[<?php echo $number; ?>][com_text]" 
    id="<?php echo $number; ?>-com-text" 
    value="<?php echo $com_text; ?>"/></td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-video-links">
  Allow YouTube Videos?
  </label></td>
<td><input class="widefat" type="checkbox" 
    name="gfc-wall-post[<?php echo $number; ?>][video_links]" 
    id="<?php echo $number; ?>-video-links" 
    value="video," <?php if ( "video," == $video_links ){ echo 'checked="checked"'; } ?> /> </td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-anon-post">
  Allow Anonymous Posts?
  </label></td>
<td><input class="widefat" type="checkbox" 
    name="gfc-wall-post[<?php echo $number; ?>][anon_post]"
    id="<?php echo $number; ?>-anon-post" 
    value="true" <?php if ( "true" == $anon_post ){ echo 'checked="checked"'; } ?> /> </td>
</tr>
<tr><td><label for="<?php echo $number; ?>-scope">
Wall Gadget Scope:
  </label></td>
  <td><select id="<?php echo $number; ?>-scope" name="gfc-wall-post[<?php echo $number; ?>][scope]"><?php echo "<option value='SITE' ".($scope=="SITE" ? "selected='selected'" : '').">Entire Site</option>"; ?><?php echo "<option value='PAGE' ".($scope=="PAGE" ? "selected='selected'" : '').">Page</option>"; ?><?php echo "<option value='ID' ".($scope=="ID" ? "selected='selected'" : '').">Item</option>"; ?></select></td></tr>
<tr>
<td><label for="<?php echo $number; ?>-wallid">
<b>If Scope=Item</b>, Enter an ID:
  </label></td>
<td><input class="widefat" type="text" 
    name="gfc-wall-post[<?php echo $number; ?>][wallid]" 
    id="<?php echo $number; ?>-wallid" 
    value="<?php echo $wallid; ?>"/></td>
</tr>
  
  </table>
<h3>Wall Color Settings</h3>
<table>
<tr>
<td>
<table>
<tr><td>Format Example:</td><td><b>FFFFFF</b></td></tr>
<tr><td><label for="<?php echo $number; ?>-endcap-bkgd">
Endcap Bkgd:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][endcap_bkgd]"
    type="text"
    id="<?php echo $number; ?>-endcap-bkgd"
    value="<?php echo $endcap_bkgd; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-endcap-text">
Endcap Text:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][endcap_text]"
    type="text"
    id="<?php echo $number; ?>-endcap-text"
    value="<?php echo $endcap_text; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-endcap-links">
Endcap Links:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][endcap_links]"
    type="text"
    id="<?php echo $number; ?>-endcap-links"
    value="<?php echo $endcap_links; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-content-bkgd">
Content Bkgd:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_bkgd]"
    type="text"
    id="<?php echo $number; ?>-content-bkgd"
    value="<?php echo $content_bkgd; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-content-text">
Content Text:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_text]"
    type="text"
    id="<?php echo $number; ?>-content-text"
    value="<?php echo $content_text; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-content-text2">
Sec. Content Text:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_text2]"
    type="text"
    id="<?php echo $number; ?>-content-text2"
    value="<?php echo $content_text2; ?>"
    size="7"/></td>
</tr>
</table>
</td>
<td>
<table>
<tr><td><label for="<?php echo $number; ?>-content-links">
Content Links:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_links]"
    type="text"
    id="<?php echo $number; ?>-content-links"
    value="<?php echo $content_links; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-content-links2">
Sec. Content Links:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_links2]"
    type="text"
    id="<?php echo $number; ?>-content-links2"
    value="<?php echo $content_links2; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-content-hlines">
Content Headlines:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][content_hlines]"
    type="text"
    id="<?php echo $number; ?>-content-hlines"
    value="<?php echo $content_hlines; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-alt-bkgd">
Alt. Background:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][alt_bkgd]"
    type="text"
    id="<?php echo $number; ?>-alt-bkgd"
    value="<?php echo $alt_bkgd; ?>"
    size="7"/></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-gfci-border">
Border:</label></td>
<td><input class="widefat"
    name="gfc-wall-post[<?php echo $number; ?>][gfci_border]"
    type="text"
    id="<?php echo $number; ?>-gfci-border"
    value="<?php echo $gfci_border; ?>"
    size="7"/></td>
    <td><input type="hidden" id="gfc-wall-submit-<?php echo $number; ?>" name="gfc-wall-submit-<?php echo $number; ?>" value="1" /></td>
</tr>
</table></td></tr></table>
<?php
}
?>
